﻿using FCN;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp2.Layer
{
  public  class MulLayer
    {
        float[,]  xx , yy ;
        public float[,] forward(float[,] x, float[,] y)
        {
            xx = x;
            yy = y;
           
            return Matrix.multiply(xx,yy);
        }
        public float[,] forward(float[,] x, float _y)
        {
            xx = x;
            y = _y;

            return Matrix.multiply(xx, y);
        }
        public float[,] backward(float[,] dout)
        {
            float[,] dx;
            if (yy!=null)
              dx = Matrix.multiply(yy, dout);
            else
               dx = Matrix.multiply(dout,y);
            return dx;
        }
        public Matrix[] forward(Matrix[] x, float _y)
        {
            convx1 = x;
            y = _y;


            return Matrix.multiply(x, y);
        }
        public Matrix[] convx1, convy1;
        public Matrix[,] convxx, convyy;
        public Matrix[] forward(Matrix[] x, Matrix[] y)
        {
            convx1 = (x);
            convy1 =(y);


            return Matrix.multiply(x, y);
        }
        public Matrix[] backward(Matrix[] dout)
        {
            Matrix[] dx;
            if (convy1 != null)
                dx = Matrix.multiply(dout, convy1);
            else
                dx = Matrix.multiply(dout, y);
            return dx;
        }
        public float backward(int index,int x,int _y,float dout)
        {
           float dx;
            if (convy1 != null)
                dx = convy1[index].values[x,_y]*dout;
            else
                dx = dout* y;
            return dx;
        }
       
        public Matrix[,] forward(Matrix[,] x, float _y)
        {
            convxx = x;
            y = _y;


            return Matrix.multiply(x, y);
        }

      
        public Matrix[,] forward(Matrix[,] x, Matrix[,] y)
        {
            convxx = x;
            convyy = y;


            return Matrix.multiply(x, y);
        }
        public Matrix[,] backward(Matrix[,] dout)
        {
            Matrix[,] dx;
            if (convyy!=null)
             dx = Matrix.multiply(dout, convyy);
             else
                dx = Matrix.multiply(dout, y);
            return dx;
        }
        public float backward(int index, int indexy, int x, int _y, float dout)
        {
            float dx;
            if (convyy != null)
                dx = convyy[index, indexy].values[x, _y] * dout;
            else
                dx = dout * y;
            return dx;
        }
        public float backwardY(int index, int x, int _y, float dout)
        {
            float dx;
            if (convyy != null)
                dx = dout * convx1[index].values[x, _y];
            else
                dx = dout * x;
            return dx;
        }
        public float backwardY(int index, int indexy, int x, int _y, float dout)
        {
            float dx;
            if (convyy != null)
                dx = dout * convxx[index, indexy].values[x, _y];
            else
                dx = dout * x;
            return dx;
        }
        public Matrix[,] backwardY(Matrix[,] dout)
        {
            Matrix[,] dx;
            if (convyy != null)
                dx = Matrix.multiply(dout, convxx);
            else
                dx = Matrix.multiply(dout, x);
            return dx;
        }
        
        float x=0,y=0;
        public float forward(float _x,float _y)
        {

             x = _x;
             y= _y;
            return x*y;
        }
        public float backward(float dout)
        {
            float dx = dout * y;

            return dx;
        }
    }
}
